package org.martin.snsrs.actions;

import java.io.File;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.martin.snsrs.beans.Activity;
import org.martin.snsrs.beans.ActivityTags;
import org.martin.snsrs.beans.Tags;
import org.martin.snsrs.daos.ActivityDao;
import org.martin.snsrs.daos.TagsDao;
import org.martin.snsrs.services.UserServices;
import org.springframework.context.ApplicationContext;

import com.opensymphony.xwork2.ActionContext;

public class AddNewActivity extends UserBase{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4738229524916184813L;

	private String activityDesc;
	
	private String activityName;
	
	private String activityTags;
	
	private String sessionKey;
	
	private String activityStartTime;
	
	private String activityEndTime;

	private ActivityDao activityDao;
	
	private UserServices userService;
	
	private Integer activityType;
	
	private Date activityStartDate;
	
	private Date activityEndDate;
	
	private File activityPic;
	
	private boolean isValidate;
	
	private TagsDao tagsDao;
	
	public void validate(){
		isValidate = true;
		if(userService.getUserBySessionKey(sessionKey) == null){
			status = "error";
			errorReason = "未知用户";
			isValidate = false;
			return;
		}
		if(activityDesc == null){
			status = "error";
			errorReason = "活动详情为空";
			isValidate = false;
			return;
		}
		if(activityName == null){
			status = "error";
			errorReason = "活动名为空";
			isValidate = false;
			return;
		}
		if(activityTags == null){
			status = "error";
			errorReason = "活动标签为空";
			isValidate = false;
			return;
		}
		if(activityType == null){
			status = "error";
			errorReason = "活动类型为空";
			isValidate = false;
			return;
		}
		if(activityDao.getActivityByName(activityName) != null){
			status = "error";
			errorReason = "已有相似活动";
			isValidate = false;
			return;
		}
		
		if(activityStartTime == null){
			status = "error";
			errorReason = "活动时间为空";
			isValidate = false;
			return;
		}
		
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINA);
		try {
			activityStartDate = dateFormat.parse(activityStartTime);
			activityEndDate = dateFormat.parse(activityEndTime);
		} catch (ParseException e) {
			status = "error";
			errorReason = "未知时间格式";
			isValidate = false;
			return;
		}
	}
	
	public String getActivityStartTime() {
		return activityStartTime;
	}

	public void setActivityStartTime(String activityStartTime) {
		this.activityStartTime = activityStartTime;
	}

	public String execute(){
		if(!isValidate)
			return ERROR;
		String[] tStrings = activityTags.split(",");
		List<Tags> tags = new ArrayList<Tags>();
		for(String tag : tStrings){
			Tags tgs = new Tags();
			tgs.setTagDesc(null);
			tgs.setTagName(tag);
			tags.add(tgs);
		}
		tagsDao.saveNewTags(tags);
		Activity activities = new Activity();
		activities.setActivityDesc(activityDesc);
		activities.setActivityType(activityType);
		activities.setActivityName(activityName);
		activities.setCreateUser(userService.getUserBySessionKey(sessionKey));
		activities.setActivityStartDate(activityStartDate);
		activities.setActivityEndDate(activityEndDate);
		activityDao.saveActivity(activities);
		activities = activityDao.getActivityByName(activityName);
		for(Tags tag : tags){
			ActivityTags activityTags = new ActivityTags();
			Tags tags2 = tagsDao.getTagByName(tag.getTagName());
			activityTags.setActivities(activities);
			activityTags.setTags(tags2);
			tagsDao.saveActivityTags(activityTags);
		}
		status = "success";
		return SUCCESS;
	}

	public String getActivityDesc() {
		return activityDesc;
	}

	public void setActivityDesc(String activityDesc) {
		this.activityDesc = activityDesc;
	}

	public String getActivityName() {
		return activityName;
	}

	public void setActivityName(String activityName) {
		this.activityName = activityName;
	}

	public String getActivityTags() {
		return activityTags;
	}

	public void setActivityTags(String activityTags) {
		this.activityTags = activityTags;
	}

	public ActivityDao getActivityDao() {
		return activityDao;
	}

	public void setActivityDao(ActivityDao activityDao) {
		this.activityDao = activityDao;
	}
	
	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public void setUserService(UserServices userServices) {
		this.userService = userServices;
	}

	public Integer getActivityType() {
		return activityType;
	}

	public void setActivityType(Integer activityType) {
		this.activityType = activityType;
	}

	public String getActivityTime() {
		return activityStartTime;
	}

	public void setActivityTime(String activityTime) {
		this.activityStartTime = activityTime;
	}

	public TagsDao getTagsDao() {
		return tagsDao;
	}

	public void setTagsDao(TagsDao tagsDao) {
		this.tagsDao = tagsDao;
	}

	public String getActivityEndTime() {
		return activityEndTime;
	}

	public void setActivityEndTime(String activityEndTime) {
		this.activityEndTime = activityEndTime;
	}

	public File getActivityPic() {
		return activityPic;
	}

	public void setActivityPic(File activityPic) {
		this.activityPic = activityPic;
	}
	
	
}
